﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelDinhMuc.Model;
namespace TinhNguyenVatLieuSanBay
{
    public partial class FrmDungCu : Form
    {
        public static FrmDungCu frmdungcu = null;
        IList<DungCu> lstdungcu;
        IList<Catalog> lstdanhmuc;
        DINHMUCSANBAYDB db = new DINHMUCSANBAYDB();
        bool themorcapnhat = false;
        public FrmDungCu()
        {
            InitializeComponent();
            frmdungcu = this;
            InitDungCu();
            InitDanhMuc();
        }

        private void InitDungCu()
        {
            gvDungCu.AutoGenerateColumns = false;
            lstdungcu = db.SelectAllDungCu().ExecuteTypedList<DungCu>();
            if (lstdungcu != null && lstdungcu.Count > 0)
            {
                dungCuBindingSource.DataSource = lstdungcu;
            }
        }

        private void InitDanhMuc()
        {
            lstdanhmuc = db.selectcatalogParent().ExecuteTypedList<Catalog>();
            if (lstdanhmuc != null && lstdanhmuc.Count > 0)
            {
                catalogBindingSource.DataSource = lstdanhmuc;
            }
        }

        private void InsertUpdateDungCu()
        {
            string madungcu = txtmadungcu.Text.ToUpper().Trim();
            string tendungcu = txttendungcu.Text;
            string donvi = txtdonvitinh.Text.ToUpper().Trim();
            long idcatalog =long.Parse(cmbdanhmuc.SelectedValue.ToString());
            long id;
            try
            {
                id=long.Parse(gvDungCu.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
            }
            catch
            {
                id=0;
            }
            if (themorcapnhat == true)
            {
                foreach (var item in lstdungcu)
                {
                    if (item.MaDungCu == madungcu)
                    {
                        MessageBox.Show("Trùng Mã Dụng Cụ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtmadungcu.Focus();
                        return;
                    }
                }
            }

            IList<DungCu> lst;
            lst = db.InsertUpdateDungCu(id, idcatalog, madungcu, tendungcu, donvi).ExecuteTypedList<DungCu>();
            if (lst != null && lst.Count > 0)
            {
                InitDungCu();
            }
            themorcapnhat = false;
        }

        private void DeletedungCu()
        {
            try
            {
                long id = long.Parse(gvDungCu.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
                db.DeleteDungCu(id).Execute();
                InitDungCu();
            }
            catch
            {

            }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<DungCu> lst;
            lst=lstdungcu.Where(x => x.TenDungCu.Contains(txttimkiem.Text.ToUpper())).ToList();
            dungCuBindingSource.DataSource = lst;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmDungCu_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmdungcu = null;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            themorcapnhat = true;
            txtmadungcu.Clear();
            txtmadungcu.Focus();
            txttendungcu.Clear();
            txtdonvitinh.Clear();
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            InsertUpdateDungCu();
        }

        private void gvDungCu_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void gvDungCu_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                var item = gvDungCu.CurrentRow.DataBoundItem as DungCu;
                txtmadungcu.Text = item.MaDungCu;
                txttendungcu.Text = item.TenDungCu;
                txtdonvitinh.Text = item.DonVi;
                cmbdanhmuc.SelectedValue = item.IDCataLog;
            }
            catch
            {

            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DeletedungCu();
        }
    }
}
